﻿using System;
using System.Collections.Generic;
using System.Text;

using Csla;
using Csla.Data;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace buildingLib.daily
{
    [Serializable()]
    public class FTP_File : BusinessBase<FTP_File>
    {
        #region Bussiness
        Guid m_aid;

        public Guid AID
        {
            get { return m_aid; }
        }

        string m_name = string.Empty;

        public string Name
        {
            get { return m_name; }
            set
            {
                if (value == null) value = string.Empty;
                if (value != m_name)
                {
                    m_name = value;
                    PropertyHasChanged();
                }
            }
        }

        string m_path = string.Empty;
        /// <summary>
        /// 分类
        /// </summary>
        public string Path
        {
            get { return m_path; }
            set
            {
                if (value == null) value = string.Empty;
                if (value != m_path)
                {
                    m_path = value;
                    PropertyHasChanged();
                }
            }
        }

       

        DateTime m_archiveDate = DateTime.Now;
        public DateTime ArchiveDate
        {
            get { return m_archiveDate; }
            set
            {
                if (m_archiveDate != value)
                {
                    m_archiveDate = value;
                    PropertyHasChanged();
                }
            }
        }

       

        protected override object GetIdValue()
        {
            return m_aid;
        }

        
        #endregion


        #region Validation Rules

        protected override void AddBusinessRules()
        {
            ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "Name");
            ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "Path");
            //ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "ParkID");
            //ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "CarID");
            //ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "Contactor");
            //ValidationRules.AddRule(Csla.Validation.CommonRules.StringRequired, "Tel");

        }
        #endregion


        #region Factory Methods
        private FTP_File()
        {

        }
        public static FTP_File NewFile()
        {
            return DataPortal.Create<FTP_File>();
        }

        public static FTP_File GetFile(int id)
        {
            return DataPortal.Fetch<FTP_File>(new Criteria(id));
        }
        #endregion

        #region Data Access

        [Serializable()]
        private class Criteria
        {
            private int m_id;
            public int Id
            {
                get
                { return m_id; }
            }
            public Criteria(int id)
            { m_id = id; }
        }

        protected override void DataPortal_Create()
        {
            // TODO: load default values into object
            m_aid = Guid.NewGuid();
           
            ValidationRules.CheckRules();
        }

        
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            // TODO: insert object's data
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetSqlStringCommand("insert into ftp_file (name,path,createTime,archiveid) values(@name,@path,@createTime,@archiveid)");
            DoInsertUpdate(db, dbcom);

            //保存文件列表
            
        }

        protected override void DataPortal_Update()
        {
            // TODO: update object's data
        }

        private void DoInsertUpdate(Database db, DbCommand dbcom)
        {
            db.AddInParameter(dbcom, "name", DbType.String, Name);
            db.AddInParameter(dbcom, "path", DbType.String, Path);
            db.AddInParameter(dbcom, "createTime", DbType.DateTime, ArchiveDate);
            db.AddInParameter(dbcom, "archiveid", DbType.Guid, AID);
           
            //db.AddInParameter(dbcom, "memo", DbType.String, m_memo);
            //db.AddInParameter(dbcom, "source", DbType.String, m_source);
            //db.AddInParameter(dbcom, "archivedate", DbType.DateTime, m_archiveDate);
            //db.AddInParameter(dbcom, "logger", DbType.String, m_operator);
            //db.AddInParameter(dbcom, "modified", DbType.DateTime, DateTime.Now);
            db.ExecuteNonQuery(dbcom);
        }

        private void DataPortal_Fetch(Criteria criteria)
        {
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetSqlStringCommand("select * from ftp_file where id = @id");
            db.AddInParameter(dbcom, "id", DbType.Int32, criteria.Id);
            
            using (SafeDataReader dr = new SafeDataReader(db.ExecuteReader(dbcom)))
            {
                if (dr.Read())
                {
                    m_aid = dr.GetGuid("archiveid");
                    m_name = dr.GetString("name");
                    m_path = dr.GetString("path");
                    m_archiveDate = dr.GetDateTime("createTime");
                }
            }
        }
       
        #endregion
    }
}
